package handler

import (
	"context"
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	jwt2 "github.com/golangblogs/gojwt"
	"math/rand"
	"net/http"
	"short_video/api/request"
	"short_video/consts"
	"short_video/global"
	"short_video/internal/dao"
	"short_video/internal/model"
	"short_video/pkg"
	"strconv"
	"time"
)

func SendSms(c *gin.Context) {
	var req request.SendSms

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	RandSms := rand.Intn(900000) + 100000
	//get := global.RDB.Get(context.Background(), GetMobile(req.Mobile))
	//if get != nil {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": 10000,
	//		"msg":  "短信发送频率",
	//		"data": nil,
	//	})
	//	return
	//}
	pkg.SendSms(req.Mobile, strconv.Itoa(RandSms))

	global.RDB.Set(context.Background(), GetMobile(req.Mobile), RandSms, time.Minute*5)

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "短信发送成功",
		"data": true,
	})
}

func GetMobile(mobile string) string {
	return fmt.Sprintf("%s", mobile)
}

func Login(c *gin.Context) {
	var req request.LoginReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	get := global.RDB.Get(context.Background(), GetMobile(req.Mobile))
	if get.Val() != req.SendSms {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "获取短信失败",
			"data": nil,
		})
		return
	}
	var user model.User
	dao.GetOneByFields(&model.User{Mobile: req.Mobile}, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	token, err := jwt2.NewJWT(consts.JWT_KEY).CreateToken(jwt2.CustomClaims{
		ID: c.GetUint("userId"),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().UnixMilli() + 3600*24*3,
		},
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "获取短信失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "登录成功",
		"data": token,
	})
}

func GetUploadToken(c *gin.Context) {
	token := c.Request.Header.Get("x-token")
	refreshToken, err := jwt2.NewJWT(consts.JWT_KEY).RefreshToken(token)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "删除token失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "更新token成功",
		"data": refreshToken,
	})
}

func GetUserList(c *gin.Context) {
	var req request.GetUserList

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	//pageDto := dao.PageDto{
	//	Page:     req.Page,
	//	PageSize: req.PageSize,
	//	OrderBy:  "",
	//}
	//var list []model.User
	//paginate, err := dao.Paginate(global.DB.Where(req.Mobile), pageDto, &list)
	//if err != nil {
	//	return
	//}
	//c.JSON(http.StatusOK, gin.H{
	//	"code": http.StatusOK,
	//	"msg":  "更新token成功",
	//	"data": paginate,
	//})
}

func DetailsUser(c *gin.Context) {
	var req request.DetailsUser

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	dao.GetOneById(req.Id, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户订单详情失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "用户详情成功",
		"data": user,
	})
}

func UploadUser(c *gin.Context) {
	var req request.UploadUser

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息失败",
			"data": err.Error(),
		})
		return
	}
	dao.Update(&model.User{
		Id:     req.Id,
		Status: req.Status,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "冻结或解冻用户",
		"data": nil,
	})
}
